package com.yeyks.controller.room;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.Login;
import com.yeyks.annotation.LoginUser;
import com.yeyks.annotation.SystemLog;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.constants.SysLogMsg;
import com.yeyks.commonReference.service.hotel.RoomInfoService;
import com.yeyks.hotel.dal.domain.RoomCalendarJipInfo;
import com.yeyks.hotel.dal.domain.RoomInfo;
import com.yeyks.hotel.param.UpdateRoomCalendarBatchParam;
import com.yeyks.commonReference.service.calendar.RoomCalendarJipInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "VIP精选酒店房间模块")
@RestController
@Slf4j
@RequestMapping("/admin/room/jip")
public class JIPRoomController {

    @Autowired
    private RoomCalendarJipInfoService roomCalendarJipInfoService;
    @Autowired
    private RoomInfoService roomInfoService;

    @ApiOperation(value = "查询精选VIP房态信息", notes = "查询VIP房态信息")
    @RequestMapping(value = "/selectRoomJIPCalendar", method = RequestMethod.POST)
    public AjaxResponseBody selectRoomJIPCalendar(@RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId) {

        List<RoomCalendarJipInfo> list = roomCalendarJipInfoService.selectVIPRoomCalendar(roomId);

        return AjaxResponseBody.success(list);
    }

    @ApiOperation(value = "修改精选房间配置信息", notes = "修改精选房间配置信息")
    @RequestMapping(value = "/updateJIPRoomConfig", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateJIPRoomConfig(
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "salePrice", defaultValue = "", required = true) Integer salePrice,
            @RequestParam(name = "purcPrice", defaultValue = "", required = true) Integer purcPrice,
            @RequestParam(name = "count", defaultValue = "", required = true) Integer count) {

        RoomInfo roomInfo = roomInfoService.getById(roomId);

        if (null == roomInfo) {
            return AjaxResponseBody.failed("对象不存在");
        }
        roomInfo.setVipDiscountPrice(salePrice);
        roomInfo.setVipPrice(purcPrice);
        roomInfo.setVipCount(count);
        boolean result = roomInfoService.updateById(roomInfo);
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "修改节假日精选房间配置信息", notes = "修改节假日精选房间配置信息")
    @RequestMapping(value = "/updateHolidayJIPRoomConfig", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateHolidayJIPRoomConfig(
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "holidayBuyPrice", defaultValue = "", required = true) Integer holidayBuyPrice,
            @RequestParam(name = "holidaySalePrice", defaultValue = "", required = true) Integer holidaySalePrice,
            @RequestParam(name = "holidayCount", defaultValue = "", required = true) Integer holidayCount) {

        RoomInfo roomInfo = roomInfoService.getById(roomId);
        if (null == roomInfo) {
            return AjaxResponseBody.failed("对象不存在");
        }
        roomInfo.setHolidayBuyPrice(holidayBuyPrice);
        roomInfo.setHolidaySalePrice(holidaySalePrice);
        roomInfo.setHolidayCount(holidayCount);
        boolean result = roomInfoService.updateById(roomInfo);
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "修改单日精选VIP房态信息", notes = "修改单日VIP房态信息")
    @RequestMapping(value = "/updateRoomJIPCalendarSingle", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateRoomJIPCalendarSingle(
            @RequestParam(name = "id", defaultValue = "", required = true) Integer id,
            @RequestParam(name = "salePrice", defaultValue = "", required = true) Integer salePrice,
            @RequestParam(name = "purcPrice", defaultValue = "", required = true) Integer purcPrice,
            @RequestParam(name = "count", defaultValue = "", required = true) Integer count,
            @RequestParam(name = "status", defaultValue = "", required = true) Integer status) {

        RoomCalendarJipInfo roomCalendarJipInfo = roomCalendarJipInfoService.getById(id);

        if (null == roomCalendarJipInfo) {
            return AjaxResponseBody.failed("对象不存在");
        }
        roomCalendarJipInfo.setSalePrice(salePrice);
        roomCalendarJipInfo.setPurcPrice(purcPrice);
        roomCalendarJipInfo.setCount(count);
        roomCalendarJipInfo.setStatus(status);
        boolean result = roomCalendarJipInfoService.updateById(roomCalendarJipInfo);
        //上架操作则上架房间
        if (result && status == 1) {
            RoomInfo roomInfo = roomInfoService.selectByPrimaryKey(roomCalendarJipInfo.getRoomId());
            roomInfo.setVipStatus(1);
            roomInfoService.updateById(roomInfo);
        }
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "批量更新精选VIP房态信息", notes = "批量更新精选VIP房态信息")
    @RequestMapping(value = "/updateRoomJIPCalendarBatch", method = RequestMethod.POST)
    @Login
    public AjaxResponseBody updateRoomJIPCalendarBatch(UpdateRoomCalendarBatchParam updateRoomCalendarBatchParam, @ApiIgnore @LoginUser AccountInfo accountInfo) {

        if (null == accountInfo || !accountInfo.getType().equals(AccountType.Administrator)) {
            return AjaxResponseBody.failed("非公司账号,不允许操作");
        }
        updateRoomCalendarBatchParam.setHotelId(updateRoomCalendarBatchParam.getHotelId());
        return roomCalendarJipInfoService.updateRoomCalendarBatch(updateRoomCalendarBatchParam);
    }

    @ApiOperation(value = "初始化精选房态信息", notes = "初始精选化房态信息")
    @RequestMapping(value = "/initJIPRoomCalendar", method = RequestMethod.POST)
    public AjaxResponseBody initJIPRoomCalendar(
            @RequestParam(name = "roomId", defaultValue = "", required = true) Integer roomId,
            @RequestParam(name = "days", defaultValue = "60", required = true) Integer days) {

        RoomInfo roomInfo = roomInfoService.selectByPrimaryKey(roomId);
        if (null != roomInfo) {
            roomCalendarJipInfoService.initJIPRoomCalendar(roomInfo);
            return AjaxResponseBody.success(roomInfo);
        }
        return AjaxResponseBody.failed("失败");
    }
    
    @ApiOperation(value = "取消精选房间", notes = "取消精选房间")
    @RequestMapping(value = "/cancelJipRoom", method = RequestMethod.POST)
    @SystemLog(SysLogMsg.VIP_ROOM_CANCLE)
    public AjaxResponseBody cancelJipRoom(Integer roomId) {
        return roomInfoService.cancelVipRoom(roomId);
    }

    @ApiOperation(value = "精选房间上下架", notes = "精选房间上下架")
    @RequestMapping(value = "/updateVipRoomStatus", method = RequestMethod.POST)
    @SystemLog(SysLogMsg.VIP_ROOM_VALID)
    public AjaxResponseBody updateVipRoomStatus(Integer roomId, Integer status) {
        return roomInfoService.updateJipRoomStatus(roomId, status);
    }

    @ApiOperation(value = "精选房间信息", notes = "精选房间信息")
    @RequestMapping(value = "/selectAllRoom", method = RequestMethod.POST)
    public AjaxResponseBody selectAllRoom(Integer hotelId) {
        List<RoomInfo> vipList = roomInfoService.queryRoomListByHotelId(hotelId, 3);
        List<RoomInfo> listC2B = roomInfoService.queryRoomListByHotelId(hotelId, 0);
        vipList.addAll(listC2B);
        return AjaxResponseBody.success(vipList);
    }
}
